import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class TestPod:

    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/1_get_pod_list.yaml'))
    def test_get_pod_list(self, podinfo, get_env_info):
        # 获取pod列表接口
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default')
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/2_create_pod.yaml'))
    def test_create_pod(self, podinfo, get_env_info):
        # 创建pod接口
        baseUrl = get_env_info
        if '{namespace}' in podinfo['request']['url']:
            url = baseUrl + podinfo['request']['url'].replace('{namespace}','default')
        else:
            url = baseUrl + podinfo['request']['url']
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in podinfo['name']:
            podname=req.json()['metadata']['name']
            globals()['podname']=podname
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/3_get_specific_pod.yaml'))
    def test_get_specific_pod(self, podinfo, get_env_info):
        # 获取指定pod接口
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default').replace('{podname}',globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in podinfo['name']:
            updatereqbody = req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/4_update_pod.yaml'))
    def test_update_pod(self, podinfo, get_env_info):
        # 更新指定pod接口
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default').replace('{podname}',globals()['podname'])
        method = podinfo['request']['method']
        podinfo['request']['data'] = globals()['updatereqbody']
        data = podinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/5_update_section_pod.yaml'))
    def test_update_section_pod(self, podinfo, get_env_info):
        # 部分更新指定pod的接口
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default').replace('{podname}',globals()['podname'])
        method = podinfo['request']['method']
        data = podinfo['request']['data']
        header = podinfo['request']['headers']
        if 'Content-Type' in header:
            req = RequestUtil().send_request(method, url, headers=header, json=data)
        else:
            req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/pod/6_delete_specific_pod.yaml'))
    def test_delete_specific_pod(self, podinfo, get_env_info):
        # 删除pod接口
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default').replace('{podname}',globals()['podname'])
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']